﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using TuKu.Common;
using TuKu.BLL;

namespace TuKu.DataAccess
{
   public class pageMaker
    {
        /// <summary>
        /// 生成顶部上一页，下一页
        /// </summary>
        /// <param name="pagenumber">总页数</param>
        /// <param name="currentNumber">1 为index.html页</param>
        /// <returns></returns>
        public static string MakepagePager(int pagenumber, int currentNumber)
        {
            StringBuilder sb = new StringBuilder();
            if (currentNumber == 1)
            {
                sb.Append("<a class='on'>1</a>");
                for (int i = 2; i <= pagenumber; i++)
                {
                    sb.Append("<a href='" + i.ToString() + ".html'>" + i.ToString() + "</a>");
                }
            }
            else
            {
                for (int i = 1; i <= pagenumber; i++)
                {
                    if (currentNumber == i)
                    {
                        sb.Append("<a class='on'>" + i.ToString() + "</a>");
                    }
                    else
                    {
                        if (i == 1)
                        {
                            sb.Append("<a href='index.html'>" + i.ToString() + "</a>");
                        }
                        else
                        {
                            sb.Append("<a href='" + i.ToString() + ".html'>" + i.ToString() + "</a>");
                        }
                    }
                }
            }
            return sb.ToString();
        }

        /// <summary>
        /// 获得模板信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static string getTemplateByid(int id)
        {
            TuKu.BLL.urltemplate bll = new TuKu.BLL.urltemplate();
            TuKu.Model.urltemplate model = bll.GetModel(id);
            return model.tcontent;
        }

        public static void makeIndex(int id, string strTemplate, int urlcateid, DataTable dtleft, DataTable dtbottom, DataTable subtable)
        {

            string strsql = "select id,url,title,picpage,categoryid,pagenumber from urltable where categoryName='"+id.ToString()+"'";
            DataTable dt = Maticsoft.DBUtility.DbHelperOleDb.Query(strsql).Tables[0];
            string path = string.Empty;
            StringBuilder sb = new StringBuilder();
            foreach (DataRow dr in dt.Rows)
            {
                path = "d:\\pps\\pics\\" + dr["url"].ToString().Replace("http://tu.pps.tv/pics/", "").Replace("/index.html", "");
                if (!Directory.Exists(path))
                {
                    Directory.CreateDirectory(path);
                }
                //model = bll.GetModel(int.Parse(dr["id"].ToString()));
                sb.Append(strTemplate);
                sb.Replace("$title$", dr["title"].ToString());
                sb.Replace("$GoogleSearch_01$", TuKu.Common.Tag.GoogleSearch_01());
                sb.Replace("$GoogleAd_728x90_01$", TuKu.Common.Tag.GoogleAd_728x90_01());
                sb.Replace("$GoogleAd_728x15_01$", TuKu.Common.Tag.GoogleAd_728x15_01());
                sb.Replace("$GoogleAd_728x15_02$", TuKu.Common.Tag.GoogleAd_728x15_02());
                sb.Replace("$picPage$", dr["picPage"].ToString());
                sb.Replace("$pagePager$", TuKu.DataAccess.pageMaker.MakepagePager(int.Parse(dr["pagenumber"].ToString()), 1));
                sb.Replace("$pageIndex$", "1");
                sb.Replace("$nextUrl$", "2.html");
                sb.Replace("$Encategory$", TuKu.Common.Tag.encategory[int.Parse(dr["categoryid"].ToString())]);
                sb.Replace("$Cncategory$", TuKu.Common.Tag.cncategory[int.Parse(dr["categoryid"].ToString())]);
                //右侧列表
                sb.Replace("$rightList$", RandomList(dtleft, 6));
                //底部列表
                sb.Replace("$bottomList$", RandomList(dtbottom, 11));

                //http://upload.ppstream.com/baike/tu/et/2008-04/12077401274536481_m_4536483_1540.jpg

                sb.Replace("upload.ppstream.com", "www.tujuduo.com/upload");//更换域名服务器

                FileManager.WriteFile(path + "\\index.html", sb.ToString(), false);
                sb.Remove(0, sb.Length);
                CreateSubPage(int.Parse(dr["url"].ToString().Replace("http://tu.pps.tv/pics/", "").Replace("/index.html", "")), int.Parse(dr["pagenumber"].ToString()), strTemplate,subtable,dtleft,dtbottom);
            }
        }

        public static void CreateSubPage(int urlid, int pagenumber, string strTemplage, DataTable subtable, DataTable dtleft, DataTable dtbottom)
        {
            StringBuilder sb = new StringBuilder();
            //TuKu.BLL.urlsubtable bll = new TuKu.BLL.urlsubtable();
            //DataTable dt = bll.GetList(" rtrim(url)='"+urlid.ToString()+"'").Tables[0];
            DataTable dt = Maticsoft.DBUtility.DbHelperOleDb.Query("select * from urlsubtable where rtrim(url)='" + urlid.ToString() + "'").Tables[0];
            string path = string.Empty;
            foreach (DataRow dr in dt.Rows)
            {
                path = "d:\\pps\\pics\\" + dr["url"].ToString();
                if (!Directory.Exists(path))
                {
                    Directory.CreateDirectory(path);
                }
                sb.Append(strTemplage);
                sb.Replace("$title$", dr["title"].ToString() + dr["currentpage"].ToString());
                sb.Replace("$GoogleSearch_01$", TuKu.Common.Tag.GoogleSearch_01());
                sb.Replace("$GoogleAd_728x90_01$", TuKu.Common.Tag.GoogleAd_728x90_01());
                sb.Replace("$GoogleAd_728x15_01$", TuKu.Common.Tag.GoogleAd_728x15_01());
                sb.Replace("$GoogleAd_728x15_02$", TuKu.Common.Tag.GoogleAd_728x15_02());
                sb.Replace("$picPage$", dr["picPage"].ToString());
                sb.Replace("$pagePager$", TuKu.DataAccess.pageMaker.MakepagePager(pagenumber, int.Parse(dr["currentpage"].ToString())));
                sb.Replace("$pageIndex$", dr["currentpage"].ToString());
                if (int.Parse(dr["currentpage"].ToString()) < pagenumber)
                {
                    sb.Replace("$nextUrl$", (int.Parse(dr["currentpage"].ToString()) + 1).ToString() + ".html");
                }
                else
                {
                    sb.Replace("$nextUrl$", "index.html");
                }
                sb.Replace("$Encategory$", TuKu.Common.Tag.encategory[int.Parse(dr["categoryid"].ToString())]);
                sb.Replace("$Cncategory$", TuKu.Common.Tag.cncategory[int.Parse(dr["categoryid"].ToString())]);
                //右侧列表
                sb.Replace("$rightList$", RandomList(dtleft, 6));
                //底部列表
                sb.Replace("$bottomList$", RandomList(dtbottom, 7));

                sb.Replace("upload.ppstream.com", "www.tujuduo.com/upload");//更换域名服务器

                FileManager.WriteFile(path + "\\" + dr["currentpage"].ToString() + ".html", sb.ToString(), false);
                sb.Remove(0, sb.Length);
            }
        }
        public static string RandomList(DataTable dt, int n)
        {
            //SELECT TOP (1) id, Title, [Content], Time FROM Knowledge ORDER BY NEWID()
            if (!dt.Columns.Contains("RowID"))
                dt.Columns.Add("RowID", typeof(string));
            foreach (DataRow row in dt.Rows)
            {
                row["RowID"] = Guid.NewGuid().ToString();
            }
            DataView view = new DataView(dt);
            view.Sort = "RowID ASC";
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i <= n; i++)
            {
                sb.Append("<li>\r\n");
                sb.Append("<a href=\"" + view[i]["url"] + "\" title=\"" + view[i]["alt"] + "\" target=\"_blank\">\r\n");
                sb.Append("<img src=\"" + view[i]["img"] + "\" alt=\"" + view[i]["alt"] + "\" />\r\n");
                sb.Append("</a>\r\n");
                sb.Append("<span><a href=\"" + view[i]["url"] + "\" title=\"" + view[i]["alt"] + "\" target=\"_blank\">" + view[i]["alt"] + "</a></span>\r\n");
                sb.Append("</li>\r\n");

            }
            return sb.ToString();
        }


    }
}
